package horsepower;

import java.util.AbstractQueue;
import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @Author: fzykd
 * Date: 2024-04-15
 * Time: 20:37
 * User: LQH
 */


public class Demo {

    public static String formatStringA(String A,char[] arg){
        for (int i = 0; i < arg.length; i++) {
            if (A.contains("%s")){
                A = A.replaceFirst("%s", String.valueOf(arg[i]));
            }else {
                A = A + arg[i];
            }
        }
        return A;
    }

    public static String formatString (String str, char[] arg) {
        AbstractQueue<Character> queue = new PriorityQueue<>();
        for (char c : arg) {
            queue.add(c);
        }
        char[] charStr = str.toCharArray();
        for (int i = 0; i < charStr.length; i++) {
            if (charStr[i] == '%') {
                if (charStr[i + 1] == 's') {
                    charStr[i] = queue.poll();
                    i = i + 1;
                }
            }
        }
        char[] qStr = new char[queue.size()];
        for(int i = 0; i<queue.size();i++){
            qStr[i] = queue.poll();
        }
        String s1 = new String(qStr);
        String s2 = new String(charStr);
        String s3 = s2 + s1;
        char[] ch = s3.toCharArray();
        for(int i= 0;i<ch.length;i++){
            if(ch[i] == 's'){
                ch[i] = ch[i+1];
                i= i+1;
            }
        }
        String s4 = new String(ch);
        return s4;
    }

    public static void main(String[] args) {
        String str = "A%sB%sC";
        char[] arr = new char[]{'E','F','G'};
//        System.out.println(formatString(str, arr));

        String[] s = str.split("%s");
        System.out.println(s.length);
        System.out.println(Arrays.toString(s));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (i<s.length){
                sb.append(s[i]);
            }

            sb.append(arr[i]);
        }
        String string = sb.toString();
        System.out.println(string);

        System.out.println("字符串替换: " + formatStringA(str, arr));

    }
}
